{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 串到串(对照表)式的加密/解密 (第4讲的版本)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "alphabet_src = 'abcdefghijklmnopqrstuvwxyz'\n",
    "alphabet_tar = 'defghijklmnopqrstuvwxyzabc'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def encryptIt(src_str: str) -> str:\n",
    "    '''用于对字符串进行简单替换加密\n",
    "    输入参数：\n",
    "    src_str： 原始文本内容\n",
    "    返回结果：加密/解密文本\n",
    "    '''\n",
    "    global alphabet_src, alphabet_tar\n",
    "    encrypted_str = ''\n",
    "    for single_char in src_str:\n",
    "        if single_char in alphabet_src:\n",
    "            idx = alphabet_src.index(single_char)\n",
    "            encrypted_str = encrypted_str + alphabet_tar[idx]\n",
    "        else:\n",
    "            encrypted_str = encrypted_str + single_char\n",
    "    return encrypted_str\n",
    "\n",
    "def decryptIt(encrypted_str: str) -> str:\n",
    "    '''用于对字符串进行简单替换解密\n",
    "    输入参数：\n",
    "    encrypted_str： 加密文本内容\n",
    "    返回结果：解密文本\n",
    "    '''\n",
    "    global alphabet_src, alphabet_tar\n",
    "    decrypted_str = ''\n",
    "    for single_char in encrypted_str:\n",
    "        if single_char in alphabet_tar:\n",
    "            idx = alphabet_tar.index(single_char)\n",
    "            decrypted_str = decrypted_str + alphabet_src[idx]\n",
    "        else:\n",
    "            decrypted_str = decrypted_str + single_char\n",
    "    return decrypted_str\n",
    "\n",
    "#对两个函数互反性进行验证\n",
    "assert(decryptIt(encryptIt('AbCdefgH!')) == 'AbCdefgH!')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 两个函数合并成一个函数，并改成索引位置平移法实现 (第4讲的版本)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I oryh brx!\n",
      "I love you!\n"
     ]
    }
   ],
   "source": [
    "def cryptIt(src_str: str, if_decrypt: bool=False) -> str:\n",
    "    '''用于对字符串进行简单替换加密/解密\n",
    "    输入参数：\n",
    "    src_str： 原始文本内容\n",
    "    if_decrypt： True表示解密过程，False表示加密过程\n",
    "    返回结果：加密/解密文本\n",
    "    '''\n",
    "    global alphabet_src\n",
    "    result = ''\n",
    "    for single_char in src_str:\n",
    "        if single_char in alphabet_src:\n",
    "            old_index = alphabet_src.index(single_char)\n",
    "            if if_decrypt == True:\n",
    "                new_index = (old_index - 3) % 26\n",
    "            else:\n",
    "                new_index = (old_index + 3) % 26\n",
    "            result = result + alphabet_src[new_index]\n",
    "        else:\n",
    "            result = result + single_char\n",
    "    return result\n",
    "\n",
    "\n",
    "assert('abcdefghijklmnopqrstuvwxyz!' == cryptIt(cryptIt('abcdefghijklmnopqrstuvwxyz!'), True))\n",
    "\n",
    "print(cryptIt('I love you!'))\n",
    "print(cryptIt('I oryh brx!', True))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 对函数实现行数进行缩减(条件选择赋值语句+续行)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I oryh brx!\n",
      "I love you!\n"
     ]
    }
   ],
   "source": [
    "def crypt_it(src_str: str, if_decrypt: bool=False) -> str:\n",
    "    '''用于对字符串进行简单替换加密/解密\n",
    "    输入参数：\n",
    "    src_str： 原始文本内容\n",
    "    if_decrypt： True表示解密过程，False表示加密过程\n",
    "    返回结果：加密/解密文本\n",
    "    '''\n",
    "    global alphabet_src\n",
    "    result = ''\n",
    "    for single_char in src_str:\n",
    "        if single_char in alphabet_src:\n",
    "            #字符移位替换\n",
    "            new_index = (alphabet_src.index(single_char) - 3) % 26 \\\n",
    "                if if_decrypt is True \\\n",
    "                else (alphabet_src.index(single_char) + 3) % 26\n",
    "            result += alphabet_src[new_index]\n",
    "        else:\n",
    "            result += single_char\n",
    "    return result\n",
    "\n",
    "\n",
    "assert('abcdefghijklmnopqrstuvwxyz!' == crypt_it(crypt_it('abcdefghijklmnopqrstuvwxyz!'), True))\n",
    "\n",
    "print(crypt_it('I love you!'))\n",
    "print(crypt_it('I oryh brx!', True))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 行数进一步缩减"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I oryh brx!\n",
      "I love you!\n"
     ]
    }
   ],
   "source": [
    "def crypt_it(src_str: str, if_decrypt: bool=False) -> str:\n",
    "    '''用于对字符串进行简单替换加密/解密\n",
    "    输入参数：\n",
    "    src_str： 原始文本内容\n",
    "    if_decrypt： True表示解密过程，False表示加密过程\n",
    "    返回结果：加密/解密文本\n",
    "    '''\n",
    "    global alphabet_src\n",
    "    result = []\n",
    "    for single_char in src_str:\n",
    "        if single_char in alphabet_src:\n",
    "            #字符移位替换\n",
    "            new_index = (alphabet_src.index(single_char) - 3) % 26 \\\n",
    "                if if_decrypt is True \\\n",
    "                else (alphabet_src.index(single_char) + 3) % 26\n",
    "            result.append(alphabet_src[new_index])\n",
    "        else:\n",
    "            result.append(single_char)\n",
    "    return ''.join(result)\n",
    "\n",
    "\n",
    "assert('abcdefghijklmnopqrstuvwxyz!' == crypt_it(crypt_it('abcdefghijklmnopqrstuvwxyz!'), True))\n",
    "\n",
    "print(crypt_it('I love you!'))\n",
    "print(crypt_it('I oryh brx!', True))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 再缩减"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I oryh brx!\n",
      "I love you!\n"
     ]
    }
   ],
   "source": [
    "def crypt_it(src_str: str, if_decrypt: bool=False) -> str:\n",
    "    '''用于对字符串进行简单替换加密/解密\n",
    "    输入参数：\n",
    "    src_str： 原始文本内容\n",
    "    if_decrypt： True表示解密过程，False表示加密过程\n",
    "    返回结果：加密/解密文本\n",
    "    '''\n",
    "    global alphabet_src\n",
    "#     result = []\n",
    "#     for single_char in src_str:\n",
    "#         result.append(alphabet_src[(alphabet_src.index(single_char) - 3) % 26 \\\n",
    "#                 if if_decrypt is True \\\n",
    "#                 else (alphabet_src.index(single_char) + 3) % 26] if \\\n",
    "#                 single_char in alphabet_src else single_char)\n",
    "    result = [alphabet_src[(alphabet_src.index(single_char) - 3) % 26 \\\n",
    "                if if_decrypt is True \\\n",
    "                else (alphabet_src.index(single_char) + 3) % 26] if \\\n",
    "                single_char in alphabet_src else single_char \\\n",
    "                for single_char in src_str]\n",
    "    return ''.join(result)\n",
    "\n",
    "\n",
    "assert('abcdefghijklmnopqrstuvwxyz!' == crypt_it(crypt_it('abcdefghijklmnopqrstuvwxyz!'), True))\n",
    "\n",
    "print(crypt_it('I love you!'))\n",
    "print(crypt_it('I oryh brx!', True))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 最终缩减版本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I oryh brx!\n",
      "I love you!\n"
     ]
    }
   ],
   "source": [
    "def crypt_it(src_str: str, if_decrypt: bool=False) -> str:\n",
    "    '''用于对字符串进行简单替换加密/解密\n",
    "    输入参数：\n",
    "    src_str： 原始文本内容\n",
    "    if_decrypt： True表示解密过程，False表示加密过程\n",
    "    返回结果：加密/解密文本\n",
    "    '''\n",
    "    global alphabet_src\n",
    "    return ''.join([alphabet_src[(alphabet_src.index(single_char) - 3) % 26 \\\n",
    "                if if_decrypt is True \\\n",
    "                else (alphabet_src.index(single_char) + 3) % 26] if \\\n",
    "                single_char in alphabet_src else single_char \\\n",
    "                for single_char in src_str])\n",
    "\n",
    "\n",
    "assert('abcdefghijklmnopqrstuvwxyz!' == crypt_it(crypt_it('abcdefghijklmnopqrstuvwxyz!'), True))\n",
    "\n",
    "print(crypt_it('I love you!'))\n",
    "print(crypt_it('I oryh brx!', True))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 设计convert_char函数，通过局部重用，缩减行数的同时提高可读性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [],
   "source": [
    "alphabet_src = 'abcdefghijklmnopqrstuvwxyz'\n",
    "alphabet_tar = 'defghijklmnopqrstuvwxyzabc'\n",
    "\n",
    "def convert_char(single_char, operation):\n",
    "    global alphabet_src, alphabet_tar\n",
    "    result = ''\n",
    "    if single_char in alphabet_src:\n",
    "        if operation == 'encrypt':\n",
    "            result = alphabet_tar[alphabet_src.index(single_char)]\n",
    "        elif operation == 'decrypt':\n",
    "            result = alphabet_src[alphabet_tar.index(single_char)]\n",
    "    else:\n",
    "        result = single_char\n",
    "    return result\n",
    "    \n",
    "def encrypt_it(src_str: str) -> str:\n",
    "    '''用于对字符串进行简单替换加密\n",
    "    输入参数：\n",
    "    src_str： 原始文本内容\n",
    "    返回结果：加密/解密文本\n",
    "    '''\n",
    "    encrypted_str = ''\n",
    "    for single_char in src_str:\n",
    "        encrypted_str += convert_char(single_char, 'encrypt')\n",
    "    return encrypted_str\n",
    "\n",
    "def decrypt_it(encrypted_str: str) -> str:\n",
    "    '''用于对字符串进行简单替换解密\n",
    "    输入参数：\n",
    "    encrypted_str： 加密文本内容\n",
    "    返回结果：解密文本\n",
    "    '''\n",
    "    decrypted_str = ''\n",
    "    for single_char in encrypted_str:\n",
    "        decrypted_str += convert_char(single_char, 'decrypt')\n",
    "    return decrypted_str\n",
    "\n",
    "#对两个函数互反性进行验证\n",
    "assert(decrypt_it(encrypt_it('AbCdefgH!')) == 'AbCdefgH!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "khoor zruog!\n",
      "hello world!\n"
     ]
    }
   ],
   "source": [
    "print(encrypt_it('hello world!'))\n",
    "print(decrypt_it('khoor zruog!'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [1, 2, 3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 2 µs, sys: 1 µs, total: 3 µs\n",
      "Wall time: 6.2 µs\n"
     ]
    }
   ],
   "source": [
    "%time\n",
    "b = []\n",
    "for e in a:\n",
    "    b.append(e + 1)\n",
    "a = b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 3 µs, sys: 0 ns, total: 3 µs\n",
      "Wall time: 5.25 µs\n"
     ]
    }
   ],
   "source": [
    "%time\n",
    "a = [e**2 for e in a]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[4, 9, 16]"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [],
   "source": [
    "a.append(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3]"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = [1, 1, None, 1 ,[1,2], 'sda', 4, print]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4564590176"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "id(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "a[7] = 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4564590176"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "id(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "def mytest(ele):\n",
    "    ele[0] = 9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "mytest(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[9, 1, None, 1, [1, 2], 'sda', 4, 0]"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['1', '2', '3', '', '', '4', '5', '6', '7\\n8\\t9']"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'1 2 3   4 5 6 7\\n8\\t9'.split(' ')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1 2 3  4 5 6 7\\n8\\t9'"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "' '.join(['1', '2', '3', '', '4', '5', '6', '7\\n8\\t9'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1 2 3   4 5 6 7\\n8\\t9'"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "' '.join(['1', '2', '3', '', '', '4', '5', '6', '7\\n8\\t9'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function join:\n",
      "\n",
      "join(iterable, /) method of builtins.str instance\n",
      "    Concatenate any number of strings.\n",
      "    \n",
      "    The string whose method is called is inserted in between each given string.\n",
      "    The result is returned as a new string.\n",
      "    \n",
      "    Example: '.'.join(['ab', 'pq', 'rs']) -> 'ab.pq.rs'\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(''.join)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
